<template>
    <div class="container">
      <div class="container-title">
        <p>社区导览</p>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>社区生活</el-breadcrumb-item>
          <el-breadcrumb-item>社区导览</el-breadcrumb-item>
          <el-breadcrumb-item>社区导览</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <div class="search-box">
        <el-form ref="form" :model="form">
          <el-form-item>
            <!-- 查询框 -->
              <el-input v-model="locationName" placeholder="位置名称"></el-input>
              
              <el-select v-model="timeState" filterable placeholder="类型">
                  <el-option
                    v-for="item in timeS"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
              </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="success"
              @click="onSearch"
              icon="el-icon-search"
              style="background-color: #0bb197"
              >查询</el-button
            ><el-button @click="onReset" icon="el-icon-refresh" class="reset"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>

      <!-- 表格 -->
      <div class="data-list">
        <div class="data-list-title">
          <span>查询结果列表</span>
          <el-button type="success" style="background-color: #0bb197"
           @click="addDialog = true">新增</el-button
          >
        </div>

        <!-- 新增弹出框 -->
        <el-dialog title="新增导览地" :visible="addDialog" class="new">
          <el-form :model="addData" :inline="true">
            <el-form-item label="*位置名称" :label-width="formLabelWidth" class="name">
              <el-input v-model="addData.locationName" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="*类型" :label-width="formLabelWidth" class="type">
              <el-select v-model="addData.type" placeholder="请选择">
                <el-option label="餐饮店" value="0"></el-option>
                <el-option label="娱乐店" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="*商家定位" :label-width="formLabelWidth" class="location">
              <div class="demo-image">
                <div class="block" v-for="fit in fits" :key="fit">
                  <span class="demonstration">{{ fit }}</span>
                  <el-image
                    style="width: 100px; height: 100px"
                    :src="url"
                    :fit="fit"></el-image>
                </div>
              </div>
            </el-form-item>
          </el-form>
          <div class="dialog-footer" align="center">
            <el-button @click="addDialog = false" class="off">取 消</el-button>
            <el-button type="primary" @click="add" class="on">确 定</el-button>
          </div>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="新增导览地" :visible="redDia" class="new">
          <el-form :model="redData" :inline="true">
            <el-form-item label="*位置名称" :label-width="formLabelWidth" class="name">
              <el-input v-model="redData.locationName" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="*类型" :label-width="formLabelWidth" class="type">
              <el-select v-model="redData.type" placeholder="请选择">
                <el-option label="餐饮店" value="0"></el-option>
                <el-option label="娱乐店" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="*商家定位" :label-width="formLabelWidth" class="location">
              <div class="demo-image">
                <div class="block" v-for="fit in fits" :key="fit">
                  <span class="demonstration">{{ fit }}</span>
                  <el-image
                    style="width: 100px; height: 100px"
                    :src="url"
                    :fit="fit"></el-image>
                </div>
              </div>
            </el-form-item>
          </el-form>
          <div class="dialog-footer" align="center">
            <el-button @click="redDia = false" class="off">取 消</el-button>
            <el-button type="primary" @click="edit" class="on">确 定</el-button>
          </div>
        </el-dialog>

        <!-- 表单内容 -->
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="cgId" label="序号" width="180" align="center"></el-table-column>
          <el-table-column prop="locationName" label="位置名称" width="180" align="center"></el-table-column>
          <el-table-column prop="latitudeLongitude" label="经纬度" align="center"> </el-table-column>
          <el-table-column prop="type" label="类型" align="center"> 
            <template slot-scope="scope">
              <span>{{ scope.row.type == 0 ? "餐饮店" : "娱乐店" }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center"> 
            <template slot-scope="scope">
              <span>{{ timeTrans(scope.row.createTime, 1) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button @click="handleEdit(scope.row)" type="text" size="small"
                >编辑</el-button
              >
              <el-button type="text" size="small" @click="deleteRow(scope.row.cgId)">删除</el-button>
            </template></el-table-column
          >
        </el-table>


        <!-- 页码 -->
        <el-pagination
          class="msg-pagination-container"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="count"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :background="true"
        >
        </el-pagination>
      </div>
    </div>
  </template>
  
  <script>
  import time from "../utils/time";
  export default {
    mixins: [time],
    data: function () {
      return {
        fits: ['fill'],
        url: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
        currentPage4: 1,
        addDialog:false,
        redDia:false,
        formLabelWidth:'90px',

        locationName:'',
        page: 1,
        count: 5,
        total: 1,
        timeState: "",

        addData:{
          locationName:'',
          type:0,

        },
        redData:{},

        timeState: "",
        timeS: [
          {
            value: 0,
            label: '餐饮店'
          }, {
            value: 1,
            label: '娱乐店'
          }
        ],

        options: [
          // {
          //   value: '0',
          //   label: '餐饮店',
          // },
          // {
          //   value: '1',
          //   label: '娱乐店',
          // },
        ],
        form: { status: '', date: '' },
        tableData: [],
      };
    },
    methods: {
      // 查询
      onSearch: function () {
        this.getData();
      },

      // 新增
      add:function(){
        this.$axios({
          url:'/smart/community-guides/add',
          method:'POST',
          data:this.addData,
        }).then(({data})=>{
          if (data.code == "00000") {
            this.$message.success("添加成功");
            this.addDialog = false;
            this.getData();
          } else {
            this.$message.error("添加失败");
          }
        })
      },

      // 删除
      deleteRow(row) {
        console.log(row);
        this.$axios({
          url: "/smart/community-guides/delete/" + row,
          method: "DELETE",

        }).then(({ data }) => {
          if (data.code == "00000") {
            this.$message.success("删除成功");
            this.getData();
          } else {
            this.$message.error("删除失败");
          }
        });
      },

      //赋值
      handleEdit(row) {
        this.redDia = true;
        let redData = { ...row };
        redData.type=redData.type+'';
        this.redData = redData;
      },

      // 编辑
      edit: function () {
        this.$axios({
          url: "/smart/community-guides/edit",
          method: "put",
          data: this.redData,
        }).then(({ data }) => {
          if (data.code == "00000") {
            this.$message.success("修改成功");
            this.redDia = false;
            this.getData();
          } else {
            this.$message.error("修改失败");
          }
        });
      },

      onReset: function () {
        this.locationName = "";
        this.timeState = "";
        this.getData();
      },
      detail: function (row) {
        // console.log(row, detail);
      },
      finish: function (row) {
        // console.log(row, finish);
      },
      handleSizeChange(val) {
        this.count = val;
        this.getData();
        // console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getData();
        // console.log(`当前页: ${val}`);
      },

      // 数据链接
      getData: function () {
        this.$axios({
          url: "/smart/community-guides/search",
          method: "post",
          params: {
            pageNum: this.page,
            pageSize: this.count,
          },
          data: {
            locationName:this.locationName,
            type:this.timeState,
          },
        }).then(({ data }) => {
          console.log(data);
          if (data.code == "00000") {
            this.tableData = data.data.pageInfo.list;
            this.total = data.data.pageInfo.total;
          } else {
            this.$message.error("数据加载失败！");
          }
        });
      },
  },
    mounted:function(){
      this.getData();
    },
  };
  </script>
  
  <style lang="less" scoped>
  .container {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    .container-title {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      p {
        color: #333333;
        font-size: 16px;
        font-weight: 400;
        font-family: 'Arial Normal', 'Arial';
      }
      .el-breadcrumb-item {
        color: rgba(148, 148, 148, 0.898039215686275);
        font-size: 12px;
        font-weight: 400;
        font-family: 'Arial Normal', 'Arial';
      }
    }
    .search-box {
      width: 98%;
      height: 80px;
      background: #ffffff;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      border: 1px solid #ffffff;
      border-radius: 5px;
      .el-form {
        width: 42%;
        background: #ffffff;
        display: flex;
        justify-content: space-around;
        align-items: center;
        .el-form-item {
          margin-bottom: 0;
          .el-input{
              width: 150px;
          }
          .el-select {
            width: 150px;
            margin-left: 10px;
          }
          .reset {
            background-color: white;
            color: #c0c4cc;
            border: 1px solid #c0c4cc;
          }
          .reset:hover {
            color: #0bb197;
          }
        }
        .el-date-editor--datetimerange.el-input__inner {
          width: 310px;
        }
      }
    }
    .data-list {
      width: 98%;
      height: 100%;
      background: #ffffff;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: space-around;
      margin-top: 20px;
      .data-list-title {
        width: 98%;
        height: 60px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        span {
          height: 16px;
          font-family: 'Arial Normal', 'Arial';
          font-weight: 700;
          font-style: normal;
          font-size: 13px;
          color: #3c4145;
        }
      }

      .dialog-footer{
        width: 720px;
        .off{
          background-color: white;
          color: #c0c4cc;
          border: 1px solid #c0c4cc;
        }
        .off:hover {
          color: #0bb197;
        }
        .on{
          background-color: #0bb197;
        }
      }
      
      .el-table {
        flex: 1;
        .el-button {
          color: #0bb197;
        }
      }
      .el-pagination {
        width: 98%;
        height: 60px;
        display: flex;
        justify-content: flex-end;
        align-items: center;
      }
    }
  }
  </style>
  <style>
  .el-pagination.is-background .el-pager li:not(.disabled):hover {
    color: #0bb197;
  }
  .el-pagination.is-background .el-pager li:not(.disabled).active {
    background-color: #0bb197;
    color: #fff;
  }
  </style>